//
// Created by ZYZ on 2020/6/15.
//
#include <stdio.h>
#include <stdlib.h>
#include "stack_use2_brackets.h"
#include "linear_stack.h"

#ifdef SELEMTYPE_CHAR

BOOL IsMatch2(char ch1, char ch2) {
    if (ch1 == '(' && ch2 == ')') {
        return TRUE;
    } else if (ch1 == '[' && ch2 == ']') {
        return TRUE;
    } else if (ch1 == '{' && ch2 == '}') {
        return TRUE;
    } else {
        return FALSE;
    }
}

//2. 括号匹配的检验
void test_brackets() {

    SqStack stack;
    char chs[80] = "[([][])]", *p;

    InitStack(stack);
    p = chs;                                             //数组名 chs 就是数组首元素的地址

    while (*p) {
        switch (*p) {
            case '(' :
            case '[' :
            case '{' :
                Push(stack, *p);
                break;

            case ')':
            case ']':
            case '}':
                if (StackEmpty(stack)) {
                    printf("Error formula, don't match.\n");
                    exit(-1);
                }

                SElemType ele;
                printf("开始获取栈顶元素 \n");
                GetTop(stack, ele);
                printf("栈顶元素为: %c ,  *p = %c \n", ele, *p);

                if (IsMatch2(ele, *p)) {
                    Pop(stack, ele);
                } else {
                    printf("Don't match.\n");
                    exit(-1);
                }
        }
        p++;
    }

    if (StackEmpty(stack)) {
        printf("Matched.\n");
    } else {
        printf("Error, don't match.\n");
    }
}
#endif